package hive;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.util.ArrayList;

public class HiveUDTF extends GenericUDTF {

    //当前列的输出数量和输出格式（1.3.1之后隐藏）
    @Override
    public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {
        //操作列的数量和名称
        ArrayList<String> filedNames = new ArrayList<String>();
        //操作列的类型
        ArrayList<ObjectInspector> filedObj = new ArrayList<ObjectInspector>();
        //第一列
        filedNames.add("x");
        filedObj.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        filedNames.add("y");
        filedObj.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        return ObjectInspectorFactory.getStandardStructObjectInspector(filedNames, filedObj);
    }

    public void process(Object[] objects) throws HiveException {
        String s = objects[0].toString();
        String[] rows = s.split(",");
        for (String row : rows) {
            String[] split = row.split(":");
            forward(split); //这里需要给数组因为是一进多出
        }
    }

    public void close() throws HiveException {

    }
}
